//
//  MSAccuntLoginController.m
//  觅尚
//
//  Created by No.8 on 2019/3/20.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSAccuntLoginController.h"
#import "MSAccuntView.h"
#import "MSLoginController.h"
#import "MSForgetController.h"
#import "MSRegisterController.h"
#import "MSTaBbarController.h"
#import "MSAcctunModel.h"

#define XMGAccount @"account"
#define XMGPwd @"pwd"
#define XMGIsAutoLogin @"isAutoLogin"
#define XMGIsRem @"isRem"
#define STORE_APPID @"1458685882"
@interface MSAccuntLoginController ()
/** 界面*/
@property(nonatomic,strong)MSAccuntView *AccuntView;


/** 数组*/
@property(nonatomic,strong)NSMutableArray *ListAarray;


/** 数组*/
@property(nonatomic,strong)NSMutableArray *MyAarray;


/** 数组*/
@property(nonatomic,strong)NSMutableDictionary *RoomAarray;


/** 数组*/
@property(nonatomic,strong)NSMutableDictionary *CaiwuDictionary;

/** <#name#>*/
@property(nonatomic,strong)NSString *updateStatus;

/** 遮盖层*/
@property(nonatomic,strong)UIView *backView;


/** 弹出View*/
@property(nonatomic,strong)UIView *showView;
@end

@implementation MSAccuntLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//     [self Versionupdate];
    
    [self AddLoginView];
    
    [self AnimationClick];
    
   
    
    self.ListAarray = [NSMutableArray new];
    self.MyAarray = [NSMutableArray new];
    
//    self.RoomAarray = [NSMutableArray new];
//    先从沙盒当中取出保存的设置
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.AccuntView.iphoneTextField.text = [defaults objectForKey:XMGAccount];
    self.AccuntView.codeTextField.text = [defaults objectForKey:XMGPwd];

    if (self.AccuntView.iphoneTextField.text.length != 0) {
         [self AnimationloginClick:self.AccuntView.loginButton];
    }
   
    
//    [self shareAppVersionAlert];
    
    
}
//判断是否需要提示更新App


- (void)GupdateStatus{
    
    if ([self.updateStatus isEqualToString:@"1"]) {
        UIView * view = [[UIView alloc] init];
        view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        [[UIApplication sharedApplication].keyWindow  addSubview:view];
        self.backView = view;
        
        
        UIView * showView = [[UIView alloc] init];
        showView.frame = CGRectMake(200 * px, ScreenHeight / 2 , ScreenWidth - 400 * px, ScreenHeight / 5);
        showView.backgroundColor = [UIColor whiteColor];
        showView.layer.cornerRadius = 20 * px;
        [view addSubview:showView];
        self.showView = showView;
        
        
        UIButton * choseButton = [[UIButton alloc] init];
        choseButton.frame = CGRectMake(showView.width - 90 * px, 20 * px, 60 * px, 60 * px);
        [choseButton setImage:[UIImage imageNamed:@"pop_dis"] forState:UIControlStateNormal];
        [choseButton addTarget:self action:@selector(choseButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [showView addSubview:choseButton];
        
        /** title*/
        UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 80 * px, showView.width, 60 * px)];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.text = @"更新最新版本";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:SYRealValue(35 / 2)];
        [showView addSubview:titleLabel];
        
        UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, showView.height / 2, showView.width, 1)];
        lineView.backgroundColor = BackViewColor;
        [showView addSubview:lineView];
        /** 确定*/
        UIButton * quuedingButton = [[UIButton alloc] initWithFrame:CGRectMake(100 * px, CGRectGetMaxY(lineView.frame) + 70 * px, showView.width / 2 - 200 * px, 60 * px)];
        [quuedingButton setTitle:@"确定" forState:UIControlStateNormal];
        quuedingButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
        [quuedingButton  setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [quuedingButton  addTarget:self action:@selector(quuedingButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [showView addSubview:quuedingButton];
        
        
        /** 确定*/
        UIButton * quxiaoButton = [[UIButton alloc] initWithFrame:CGRectMake(100 * px + showView.width / 2, quuedingButton.y, quuedingButton.width,quuedingButton.height)];
        [quxiaoButton setTitle:@"取消" forState:UIControlStateNormal];
        quxiaoButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
        [quxiaoButton  setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [quxiaoButton  addTarget:self action:@selector(quxiaoButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [showView addSubview:quxiaoButton];
    }
}
- (void)quxiaoButtonClick{
    
    [self choseButtonClick];
}
- (void)quuedingButtonClick{
   [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id1458685882"]];

    
}

- (void)choseButtonClick{
    
    [self.backView removeFromSuperview];
}
//添加界面
- (void)AddLoginView{
    
    self.AccuntView = [[MSAccuntView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.AccuntView];
    
   
    
    self.AccuntView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"登录bg"]];
    
    UIView * ImageView = [[UIView alloc] initWithFrame:self.AccuntView.frame];
    UIImage * image = [UIImage imageNamed:@"登录bg"];
    ImageView.layer.contents = (__bridge id)image.CGImage;
    /** 设置对齐模式*/
    ImageView.layer.contentsGravity = kCAGravityResize;
    [self.AccuntView addSubview:ImageView];
    
     [self.AccuntView addSubview:self.AccuntView.logintitle];
    
    [self.AccuntView addSubview:self.AccuntView.iphoneTextField];
    
    [self.AccuntView addSubview:self.AccuntView.iphonelineIMG];
    
    [self.AccuntView addSubview:self.AccuntView.codeTextField];
    
    [self.AccuntView addSubview:self.AccuntView.visibleButton];
    
    [self.AccuntView addSubview:self.AccuntView.passworldlineIMG];
    
    [self.AccuntView addSubview:self.AccuntView.loginButton];
    
    [self.AccuntView addSubview:self.AccuntView.choseButton];
    
    [self.AccuntView addSubview:self.AccuntView.forgetButton];
    
    [self.AccuntView addSubview:self.AccuntView.registerButton];
}
//添加事件
- (void)AnimationClick{
    //登录按钮
    [self.AccuntView.loginButton addTarget:self action:@selector(AnimationloginClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //手机号登录按钮
    [self.AccuntView.choseButton addTarget:self action:@selector(AnimationchoseButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    //忘记密码按钮
    [self.AccuntView.forgetButton addTarget:self action:@selector(AnimationforgetButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    //注册按钮
    [self.AccuntView.registerButton addTarget:self action:@selector(AnimationregisterButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    //密码可见按钮
    [self.AccuntView.visibleButton addTarget:self action:@selector(AnimationvisibleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}

/**
 * 点击登录按钮
 */
- (void)AnimationloginClick:(UIButton *)button{
    [self.ListAarray removeAllObjects];
    [self.MyAarray removeAllObjects];
    [self.RoomAarray removeAllObjects];
    [self.CaiwuDictionary removeAllObjects];
    [MBManager showLoading];
    WYCLogFunc;
    NSString *iphoneTextStr = [self.AccuntView.iphoneTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *passWordTextStr = [self.AccuntView.codeTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (iphoneTextStr.length != 11) {
        [MBManager showBriefAlert:@"手机号不正确!"];
        [MBManager hideAlert];
        return;
    }
    if (passWordTextStr.length == 0) {
        [MBManager showBriefAlert:@"密码不能为空"];
        [MBManager hideAlert];
        return;
    }
    NSDictionary * params = @{@"phoneNumber":iphoneTextStr,@"passWord":passWordTextStr};
    [HTTPRequest POST:MSloginPassword params:params succeed:^(id responseObject) {
        WYCLog(@"----------%@",responseObject);
        if (CodeStr) {
            [MBManager hideAlert];
            [MBManager showBriefAlert:@"登录成功"];
            
            
            //保存用户名和密码到沙盒里
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

            [defaults setObject:iphoneTextStr forKey:XMGAccount];
            [defaults setObject:passWordTextStr forKey:XMGPwd];
            [defaults synchronize];
            
            WYCAccount * Account = [[WYCAccount alloc] initWithDic:responseObject[@"data"][@"user"]];
            
            [WYCAccountTool saveAccount:Account];
            
            [WYCIsAppLoginTool saveIsAppLogin:YES];
//            NSMutableArray * array = [NSMutableArray new];
//            self.ListAarray = [MSAcctunModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
//            WYCLog(@"-----%@",self.ListAarray);
            [[NSUserDefaults standardUserDefaults] setObject:responseObject[@"list"] forKey:@"array"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            
            [[NSUserDefaults standardUserDefaults] setObject:responseObject[@"data"][@"businessName"] forKey:@"businessName"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            for (NSDictionary * dict in responseObject[@"list"]) {
//                [self.ListAarray addObject:dict[@"bme_tag"]];
                
                if ([dict[@"bme_tag"] intValue] == 2) {
                    for (NSDictionary * mydict in dict[@"muBaseAbilityList"]) {
                        [self.MyAarray addObject:mydict];
                        [[NSUserDefaults standardUserDefaults] setObject:self.MyAarray forKey:@"MyAarray"];
//                        WYCLog(@"-----%@",self.MyAarray);
                        [[NSUserDefaults standardUserDefaults] synchronize];
                    }
                }
                self.RoomAarray = [NSMutableDictionary dictionaryWithCapacity:5];
                if ([dict[@"bme_tag"] intValue] == 6) {
                    for (NSDictionary * mydict in dict[@"muBaseAbilityList"]) {
                       [self.RoomAarray setObject:mydict forKey:mydict[@"key"]];
                        id objectValue = [self.RoomAarray objectForKey:@"room_open"];
                        WYCLog(@"objectValue-------%@",objectValue);
                        [[NSUserDefaults standardUserDefaults] setObject:self.RoomAarray forKey:@"RoomAarray"];
                        [[NSUserDefaults standardUserDefaults] synchronize];
                    }
                }
                
                self.CaiwuDictionary = [NSMutableDictionary dictionaryWithCapacity:5];
                if ([dict[@"bme_tag"] intValue] == 5) {
                    for (NSDictionary * mydict in dict[@"muBaseAbilityList"]) {
                        [self.CaiwuDictionary setObject:mydict forKey:mydict[@"key"]];
                        id objectValue = [self.CaiwuDictionary objectForKey:@"room_open"];
                        WYCLog(@"objectValue-------%@",objectValue);
                        [[NSUserDefaults standardUserDefaults] setObject:self.CaiwuDictionary forKey:@"CaiwuDictionary"];
                        [[NSUserDefaults standardUserDefaults] synchronize];
                    }
                }
                
            }
            
            MSTaBbarController *  MSTabBar = [[MSTaBbarController alloc] init];
            
            [self presentViewController:MSTabBar animated:NO completion:nil];
        }else{
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            
        }
        
    } failure:^(NSError *error) {
        [MBManager hideAlert];
        [MBManager showBriefAlert:Failed];
    }];
    
}

/**
 * 手机号登录按钮
 */
- (void)AnimationchoseButtonClick{
    WYCLogFunc;
    MSLoginController * loginVC = [[MSLoginController alloc] init];
    [self presentViewController:loginVC animated:NO completion:nil];
    
}

/**
 * 忘记密码按钮
 */
- (void)AnimationforgetButtonClick{
    WYCLogFunc;
    MSForgetController * ForgetVC = [[MSForgetController alloc] init];
    [self presentViewController:ForgetVC animated:NO completion:nil];
    
}
/**
 * 密码可见
 */
- (void)AnimationvisibleButtonClick:(UIButton *)sender{
    WYCLogFunc;
    
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.AccuntView.codeTextField.secureTextEntry = NO;
    }else{
        self.AccuntView.codeTextField.secureTextEntry = YES;
    }
}
/**
 * 注册按钮
 */
- (void)AnimationregisterButtonClick{
    WYCLogFunc;
    MSRegisterController * RegisterVC = [[MSRegisterController alloc] init];
    [self presentViewController:RegisterVC animated:NO completion:nil];
}


@end
